package com.gxa.service.impl;

import com.gxa.common.JsonResult;
import com.gxa.common.PageRecv;
import com.gxa.common.R;
import com.gxa.mapper.RepositoryMapper;

import com.gxa.pojo.Repository;
import com.gxa.service.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RepositoryServiceImpl implements RepositoryService{
    @Autowired(required = false)
    private RepositoryMapper repositoryMapper;
//查询
    @Override
    public JsonResult queryRep(Integer currentPage,Integer pageSize) {
        JsonResult jsonResult=new JsonResult();
        Integer param1=(currentPage-1)*pageSize;
        Integer param2=pageSize*currentPage;
        List<Repository> repositoryList=repositoryMapper.queryRep(param1,param2);
        int count =repositoryMapper.countqueryRep() ;
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功！");
        jsonResult.setData(repositoryList);
        jsonResult.setCount(count);
        return jsonResult;
    }

//    删除
    @Override
    public R delRep(Integer id) {
        R r=new R();
        repositoryMapper.delRep(id);
        r.setCode(200);
        r.setMsg("删除成功！");
        return r;
    }

//    模糊查询
    @Override
    public JsonResult<Repository> searchRep(PageRecv<Repository> pageRecv) {
        //对查询页面参数计算
        Integer currentPage=pageRecv.getCurrentPage();
        Integer pageSize=pageRecv.getPageSize();
        Integer limit1=(currentPage-1)*pageSize;
        pageRecv.setCurrentPage(limit1);
        //获取查询条件实体
        Repository repository = pageRecv.getData();
//        System.out.println(repository);
        //调用mapper方法，返回查询结果list
        System.out.println("---进入service层---");
        List<Repository> list=repositoryMapper.searchRep(pageRecv);
        System.out.println("==sql语句==");
        Integer count=repositoryMapper.countsearchRep(pageRecv);
        //将list和count条数塞入r,响应成功返回
        JsonResult r=new JsonResult();
        r.setCode(200);
        r.setCount(count);
        r.setMsg("搜索成功");
        r.setData(list);
        return r;
    }

//修改
    @Override
    public R updateRep(Repository repository) {
        R r= new R();
        repositoryMapper.updateRep(repository);
        r.setCode(200);
        r.setMsg("修改成功!");
        return r;
    }

    @Override
    public R addRep(Repository repository) {
        R r=new R();
        repositoryMapper.addRep(repository);
        r.setCode(200);
        r.setMsg("添加成功！");
        return r;
    }

}
